package com.dm.testkotal;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.dm.testkotal.bean.Hello;

public abstract class AbsAdapter<T> extends BaseAdapter {

    private Context context; //上下文对象
    private int layoutResId; //item布局资源
    private List<T> datas; //数据源

    public AbsAdapter(Context applicationContext, int content_java, List<T> list) {
        this.context = applicationContext;
        this.layoutResId = content_java;
        this.datas = list;
    }

    @Override
    public int getCount() {
        if(datas!=null){
            return  datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder=null;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(layoutResId, parent,false);
            vHolder=new ViewHolder(convertView);

            convertView.setTag(vHolder);
        }else{
            vHolder=(ViewHolder) convertView.getTag();
        }

        bindView(vHolder,datas.get(position)); //将数据显示到item布局中

        return convertView;
    }

    public abstract void bindView(ViewHolder vHolder,T data);

    public static class ViewHolder{
        private Map<Integer,View> cacheViews;
        private View itemView;

        public ViewHolder(View itemView){
            this.itemView=itemView;
            cacheViews=new ArrayMap<>();
        }

        public View getView(int id){ //查找指定id的item中子控件
            View v=cacheViews.get(id);
            if(v==null){
                v=itemView.findViewById(id);
                if(v!=null){
                    cacheViews.put(id, v);
                }
            }

            return v;
        }
    }

}
